package vn.com.tiz.bookcase.task;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextPaint;
import android.util.Log;
import vn.com.tiz.DocumentZ.utils.StringTools;
import vn.com.tiz.core.task.AbstractCallbackableTask;

public class LoadChapterTask extends AbstractCallbackableTask<Void, Void, Integer> {
	private String strChapter = "chapter_1.txt";
	private ArrayList<String> pages = new  ArrayList<String>();
	private int maxLine = 32;
	private int widthDisplay = 700;
	private TextPaint textPaint;
	private int PageIndex = 1;
	
	public LoadChapterTask(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		textPaint = new TextPaint();
	}
	
	public LoadChapterTask(Context context,TextPaint textPaint,int chapterIndex) {
		super(context);
		// TODO Auto-generated constructor stub
		this.textPaint = textPaint;
		strChapter = "chapter_"+chapterIndex+".txt";
	}
	
	public ArrayList<String> getPages(){
		return pages;
	}

	@Override
	protected Integer doInBackground(Void... arg0) {
		// TODO Auto-generated method stub
		
		ArrayList<String> chapterlines = new ArrayList<String>();
		AssetManager assets = context.getAssets();	
		try {
		
			InputStreamReader reader = new InputStreamReader(assets.open(strChapter), "UTF-8");
			BufferedReader br = new BufferedReader(reader); 
			String line;				
			while ((line = br.readLine()) != null) {
				chapterlines.add(line);
			}						
			Log.d("TrungTT","Chapter:"+strChapter+" Total:"+chapterlines.size()+" lines.");			
			pages =  StringTools.parseIntoPages(chapterlines, maxLine,widthDisplay,textPaint);			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return 0;
	}
	

}
